package com.hmyd.hmyd_android.shop;

import java.lang.reflect.Type;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.JfmallScroe;
import com.hmyd.hmyd_android.ui.BasicActivity;
import com.hmyd.hmyd_android.ui.MyScoreActivity;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 我的商城界面
 * @author wangk
 */

@ContentView(R.layout.activity_jf_mall_my)
public class JfMallMyActivity extends BasicActivity {
	
	private static final int GET_MY_SCORE = 1101; 
	
	@ViewInject(R.id.myScore)
	private TextView myScore; // 我的积分
	@ViewInject(R.id.my_jfmall_title)
	private View my_jfmall_title;
	@ViewInject(R.id.home_title_txt)
	private TextView home_title_txt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	
	@OnClick({R.id.myMallAdd,R.id.myMallDetail,R.id.myMallMsg,R.id.myMallOrder,R.id.myMallCollect
		,R.id.my_jiang_pin})
	public void onClick(View view) {
		Intent intent = new Intent();
		switch (view.getId()) {
		case R.id.myMallAdd:
			intent.setClass(JfMallMyActivity.this, JfMallAdresActivity.class); // 地址管理
			startActivity(intent);
			break;
		case R.id.myMallDetail:
			intent.setClass(JfMallMyActivity.this, MyScoreActivity.class); // 积分明细
			startActivity(intent);
			break;
		case R.id.myMallMsg:
			intent.setClass(JfMallMyActivity.this, JfMallMessageActivity.class); // 站内信
			startActivity(intent);
			break;
		case R.id.myMallOrder:
			intent.setClass(JfMallMyActivity.this, MyOrderActivity.class); // 我的订单列表
			startActivity(intent);
			break;
		case R.id.myMallCollect:
			intent.setClass(JfMallMyActivity.this, MyCollectActivity.class); // 我的收藏列表
			startActivity(intent);
			break;
		case R.id.my_jiang_pin:
			break;
		default:
			break;
		}
	}

	@Override
	public void onPostSuccess(int postId, String msg) {
		Gson gson = new Gson();
		switch (postId) {
		case GET_MY_SCORE:
			JfmallScroe jfmallScroe = new JfmallScroe();
			Type type1 = new TypeToken<JfmallScroe>() {
			}.getType();
			jfmallScroe = gson.fromJson(msg, type1);
			if(jfmallScroe != null) {
				String score = jfmallScroe.score;
				try {
					AppBasic.totalScore = Double.parseDouble(score);
				} catch (NumberFormatException e) {
					Log.e("myActivity", "scroe fromate error!"+e);
				}
				myScore.setText(score);
			} else {
				myScore.setText("0");
				Toast.makeText(getApplicationContext(), "获取积分失败", Toast.LENGTH_LONG).show();
				Log.e("JfMallMyActivity", "获取积分失败");
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onPostFailure(int postId, String msg) {
		
	}
	
	@Override
	protected void onResume() {
		onRequest(GET_MY_SCORE, UrlConstant.GETMYSCORE, HttpMethod.GET, null);
		super.onResume();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			AppBasic.getInstance().finishCurrentActivity();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void setParams() {
		home_title_txt.setText("我的商城");
		my_jfmall_title.setOnTouchListener(touch);
		onRequest(GET_MY_SCORE, UrlConstant.GETMYSCORE, HttpMethod.GET, null);
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {
		
	}
}
